package logics
{
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import global.Managers;
	
	import net.servers.ServerManager;
	
	import vo.layout.LayoutVO;
	import vo.layout.MultiUnitDishVO;
	import vo.layout.MultiUnitVO;
	import vo.layout.PageVO;
	import vo.layout.SingleUnitVO;
	import vo.layout.UnitVO;
	
	public class LayoutManager
	{
		public var layoutVO						: LayoutVO;
		
		public function LayoutManager()
		{
		}
		public function init(serverManager:ServerManager):void {
			serverManager.subscribe("Layout", getLayoutHandler);
		}
		private function getLayoutHandler(data:Object):void {
			layoutVO = parseLayout(new XML(data));
			Managers.faceManager.diningFace.updateRecipe();
		}
		private function parseLayout(xml:XML):LayoutVO {
			var layoutVO:LayoutVO = new LayoutVO();
			var pages:Array = [];
			var xmlList:XMLList = xml.Page;
			for(var i:int = 0; i < xmlList.length(); i++) {
				var page:PageVO = parsePage(xmlList[i]);
				pages.push(page);
			}
			layoutVO.pages = pages;
			return layoutVO;
		}
		private function parsePage(xml:XML):PageVO {
			var pageVO:PageVO = new PageVO();
			pageVO.num = int(xml.@num);
			pageVO.bg = "recipe/" + xml.BG.toString();
			var units:Array = [];
			var xmlList:XMLList = xml.children();
			for(var i:int = 0; i < xmlList.length(); i++) {
				var unitVO:UnitVO;
				var unit:XML = xmlList[i];
				switch(unit.name().toString()) {
					case "Single":
						unitVO = parseSingleUnit(unit);
						units.push(unitVO);
						break;
					case "Multi":
						unitVO = parseMultiUnit(unit);
						units.push(unitVO);
						break;
				}
			}
			pageVO.units = units;
			return pageVO;
		}
		private function parseSingleUnit(xml:XML):SingleUnitVO {
			var singleVO:SingleUnitVO = new SingleUnitVO();
			
			singleVO.id = int(xml.@id);
			singleVO.x = Number(xml.@x);
			singleVO.y = Number(xml.@y);
			var tempXML:XML;
			tempXML = xml.Name[0];
			singleVO.nameX = Number(tempXML.@x);
			singleVO.nameY = Number(tempXML.@y);
			singleVO.nameWidth = Number(tempXML.@width);
			singleVO.nameHeight = Number(tempXML.@height);
			tempXML = xml.Price[0];
			singleVO.priceX = Number(tempXML.@x);
			singleVO.priceY = Number(tempXML.@y);
			singleVO.priceWidth = Number(tempXML.@width);
			singleVO.priceHeight = Number(tempXML.@height);
			singleVO.priceUnitName = tempXML.toString();
			tempXML = xml.Image[0];
			singleVO.imageX = Number(tempXML.@x);
			singleVO.imageY = Number(tempXML.@y);
			singleVO.imageWidth = Number(tempXML.@width);
			singleVO.imageHeight = Number(tempXML.@height);
			
			return singleVO;
		}
		private function parseMultiUnit(xml:XML):MultiUnitVO {
			var multiVO:MultiUnitVO = new MultiUnitVO();
			multiVO.columnNum = int(xml.@cols);
			multiVO.width = Number(xml.@width);
			multiVO.height = Number(xml.@height);
			multiVO.x = Number(xml.@x);
			multiVO.y = Number(xml.@y);
			var dishes:Array = [];
			var xmlList:XMLList = xml.Dish;
			for(var i:int = 0; i < xmlList.length(); i++) {
				var dishVO:MultiUnitDishVO = new MultiUnitDishVO();
				var dish:XML = xmlList[i];
				dishVO.id = int(dish.@id);
				dishVO.priceUnitName = dish.toString();
				dishes.push(dishVO);
			}
			multiVO.unitDishes = dishes;
			return multiVO;
		}
		
		public function getLayout():void {
			Managers.serverManager.send("GetLayout");
		}
	}
}